<?php 
/* 
 * This file is part of TCDB. 
 * 
 * Copyright (C) 2000-2009 
 * Technology Consultant Corps 
 * Grinnell College 
 * Grinnell, IA, 50112 
 * tc@grinnell.edu 
 * 
 * TCDB is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version. 
 * 
 * TCDB is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 * GNU General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License 
 * along with TCDB. If not, see <http://www.gnu.org/licenses/>. 
 * 
 
 ============================================================================= 
 
 * directory.php -- Displays information about all active users on TCDB;
 *		    information is sortable by username, name, rank, etc.
 * 
 * Author: Dylan J. Sather
 * Created: 2009-07-09 
 * Last edited: 2009-07-13 
 *
 * Thanks to Andrew Kensler for his work on directory.php since January 2001
 */ 
 
$page_title = "directory.php"; 

require('init.php'); 
require('require_login.php'); 
include('header.php'); 

// Heading

echo "  <div class='content_box'> <!-- CONTENT div -->
  <h2 class='underline'>Directory Info</h2>
    <table id='directory'>
      <tr>
	<th class='underline'><a href='directory.php?sort=0'>Username</a></th>
	<th class='underline'><a href='directory.php?sort=1'>Name</a></th>
	<th class='underline'><a href='directory.php?sort=2'>E-mail</a></th>
	<th class='underline'><a href='directory.php?sort=3'>Class</a></th>
	<th class='underline'><a href='directory.php?sort=4'>Rank</a></th>
	<th class='underline'><a href='directory.php?sort=5'>Certifications</a></th>
      </tr>\n";

// $sort_array -- lets us sort by different criteria
$sort_array = array('username', 'last_name',
	       'email', 'class',
	       'rank_id', 'certifications');

// GET the sorting order; by default, sort by username
if (isset($_GET['sort']))
    $sort = $_GET['sort'];
else
    $sort = 0;

// Pull the info from the DB

$query = sprintf("SELECT DISTINCT users.id, username,
				  CONCAT(last_name, ', ', first_name),
				  email, class, rank_id,
				  ranks.name, currently_here
		  FROM users, ranks
		  WHERE users.rank_id = ranks.id
		  ORDER BY users.currently_here DESC, %s",
	      mysqli_real_escape_string($mysqli, $sort_array[$sort]));

$result = $mysqli->query($query, MYSQLI_STORE_RESULT);

// Print info
while ($row = $result->fetch_row()) {

    list($user_id, $username, $name, $email,
	 $class, $rank_id, $rank, $currently_here) = $row;

    if ($currently_here)
	echo "  
      <tr>";
    else
	echo "  
      <tr class='abroad'>";

	echo "	  
	<td><a href='http://db.grinnell.edu/campusdir/default.asp?transmit=true&amp;email=$username'>$username</a></td>
	<td><a href='user_info.php?user_id=$user_id'>$name</a></td>
	<td>$email</td>
	<td>$class</td>
	<td>$rank</td>
	<td>";

    // Now get certification info
    $query = "SELECT name
	      FROM certifications, labs
	      WHERE user_id = $user_id
	      AND lab_id = id";

    $cert_result = $mysqli->query($query, MYSQLI_STORE_RESULT);

    while (list($lab_name) = $cert_result->fetch_row()) {
	    echo "$lab_name ";
    }

    echo "	</td>
      </tr>\n";

}

$cert_result->free();

echo "	  
    </table>
  </div> <!-- End CONTENT div -->\n";

include('footer.php'); 

?> 
